<template>
	<view class="form">
		<view class="list flex-r-b-c">
			<view class="text">姓名</view>
			<input type="text" placeholder="请输入姓名" v-model="form.name">
		</view>
		<view class="list flex-r-b-c">
			<view class="text">账号</view>
			<input type="tel" placeholder="请输入账号" v-model="form.account">
		</view>
		<view class="list flex-r-b-c">
			<view class="text">银行名称</view>
			<input style="width: 70%;" type="text" placeholder="请输入银行" v-model="form.bank_name">
		</view>
		<view class="list flex-r-b-c">
			<view class="text">开户行</view>
			<input style="width: 70%;" type="text" placeholder="请输入开户行" v-model="form.bank_address">
		</view>
		<view class="popupBtn" v-if="!isEdit" @tap.stop="getAccount">提交</view>
		<view class="popupBtn" v-else @tap.stop="editAccount">提交</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					name:'',
					account:'',
					bank_name:'',
					bank_address:'',
					id:''
				},
				isEdit:false
			};
		},
		onLoad(el) {
			this.accountCardInfo()
		},
		methods: {
			// 查看绑卡信息
			accountCardInfo(){
				let that = this
				that.$api.bindTxAccountInfo().then(res=>{
					if(res.data.data) that.form = res.data.data
					if(that.form.id){
						that.isEdit = true
					}
				})
			},
			// 编辑卡
			editAccount(){
				let that = this
				that.$api.bindTxAccount({
					id:that.form.id,
					name: that.form.name,
					account:that.form.account,
					bank_name:that.form.bank_name,
					bank_address:that.form.bank_address,
				}).then(res => {
					if(res.data.msg == '设置成功'){
						that.$utils.toast(res.data.msg)
						setTimeout(() => {
							uni.navigateBack()
						},2000)
					}
				})
			},
			// 添加卡
			getAccount(){
				let that = this
				let reg_tel = /^([1-9]{1})(\d{14,19})$/
				if (that.form.name == '') return that.$utils.toast('请输入姓名')
				if (!reg_tel.test(that.form.account)) return that.$utils.toast('请输入正确银行卡号')
				if (that.form.bank_name == '') return that.$utils.toast('请输入银行名称')
				if (that.form.bank_address == '') return that.$utils.toast('请输入开户行')
				
				that.$api.bindTxAccount({
					name: that.form.name,
					account:that.form.account,
					bank_name:that.form.bank_name,
					bank_address:that.form.bank_address,
				}).then(res => {
					if(res.data.msg == '设置成功'){
						that.$utils.toast(res.data.msg)
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						},2000)
					}
				})
			},
		}
	}
	
</script>

<style lang="less">
	.form {
		width: 90%;
		margin: 0 auto;
		margin-top: 40rpx;
	
		.list {
			width: 100%;
			height: 110rpx;
			border-bottom: 1px solid #EFEFEF;
	
			.text {
				font-size: 28rpx;
				font-weight: 400;
				color: #000;
			}
	
			input {
				text-align: right;
			}
		}
	
		.default {
			margin-top: 40rpx;
			margin-left: 30rpx;
			font-size: 28rpx;
			color: #7F8084;
			font-weight: 400;
		}
	
		.popupBtn {
			width: 100%;
			height: 96rpx;
			line-height: 96rpx;
			border-radius: 96rpx;
			background-color: #000;
			color: #fff;
			font-size: 28rpx;
			font-weight: 500;
			text-align: center;
			margin-top: 100rpx;
		}
	
		.deleteBtn {
			width: 100%;
			height: 96rpx;
			line-height: 96rpx;
			border-radius: 96rpx;
			background-color: #DCDCDC;
			color: #7F8084;
			font-size: 28rpx;
			font-weight: 500;
			text-align: center;
			margin-top: 40rpx;
			margin-bottom: 40rpx;
		}
	
	}
</style>